package com.example.administrator.x3picturetransliteration.Home.Adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.x3picturetransliteration.Base.BaseListViewAdapter;
import com.example.administrator.x3picturetransliteration.Bean.UserWorkListBean;
import com.example.administrator.x3picturetransliteration.Home.Activity.RecognitionResultsActivity;
import com.example.administrator.x3picturetransliteration.Home.Dialog.OkDialog;
import com.example.administrator.x3picturetransliteration.Home.Dialog.PreservationDialog;
import com.example.administrator.x3picturetransliteration.R;
import com.squareup.picasso.Picasso;

import java.util.Arrays;
import java.util.List;

/**
 * 文档
 * Created by Administrator on 2019/8/19.
 */

public class FileFragmentAdapter extends BaseListViewAdapter{

    private Context context;
    private List<UserWorkListBean> mData;

    private deleteCallback mdeleteCallback;

    private boolean deleteVisible = false;

    private boolean isTemp = false; //是否为草稿列表

    public FileFragmentAdapter(Context context,List<UserWorkListBean> mData,boolean isTemp,deleteCallback mdeleteCallback) {
        super(context);

        this.context = context;
        this.mData = mData;
        this.isTemp = isTemp;

        this.mdeleteCallback = mdeleteCallback;
    }

    @Override
    public int getLayoutView() {
        return R.layout.home_item_file;
    }

    @Override
    public int mCount() {
        return mData.size();
    }

    @Override
    public void logic(ViewHolder mViewHolder, View view, final int position) {

        IViewHolder mIViewHolder = (IViewHolder)mViewHolder;

        List<String> imgs = Arrays.asList(mData.get(position).getImgs().split(","));

        if (imgs.size()>0 && imgs.get(0)!=null && !"".equals(imgs.get(0))){

            Picasso.with(context)
                    .load(imgs.get(0))
                    .placeholder(R.drawable.home_advertisement)
                    .error(R.drawable.home_advertisement)
                    .into(mIViewHolder.img);
        }

        mIViewHolder.NameText.setText(mData.get(position).getName()!=null?mData.get(position).getName():"");

        mIViewHolder.TimeText.setText(mData.get(position).getGmtCreate()!=null?mData.get(position).getGmtCreate():"");

        mIViewHolder.Button1Text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new PreservationDialog(context,new PreservationDialog.hd() {
                    @Override
                    public void hdff(String content) {

                        mdeleteCallback.setName(position,content);

                    }

                    @Override
                    public String nameText() {

                        return mData.get(position).getName();
                    }

                }).show();
            }
        });

        mIViewHolder.Button2Text.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new OkDialog(context,"删除后不可恢复，您确定要删除？",new OkDialog.hd() {
                    @Override
                    public void hdff() {

                        mData.get(position).setDeleteJudge(true);
                        mdeleteCallback.delete(position);
                    }
                }).show();
            }
        });

        mIViewHolder.okImg.setVisibility(deleteVisible?View.VISIBLE:View.GONE);

        mIViewHolder.okImg.setImageResource(mData.get(position).isDeleteJudge()?R.mipmap.home_check1:R.mipmap.home_check0);

        mIViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (deleteVisible){

                    mData.get(position).setDeleteJudge(!mData.get(position).isDeleteJudge());
                    FileFragmentAdapter.this.notifyDataSetChanged();

                }else {

                    String type = null;

                    if ("4".equals(mData.get(position).getType())){

                        type = "表格识别";

                    }else if ("1".equals(mData.get(position).getType())){

                        type = "手写识别";

                    }else if ("5".equals(mData.get(position).getType())){

                        type = "文档结构化识别";
                    }

                    if (!isTemp){

                        RecognitionResultsActivity.startRecognitionResultsActivity(context,mData.get(position).getId(),null,type);

                    }else {

                        RecognitionResultsActivity.startRecognitionResultsActivity(context,null,mData.get(position).getId(),type);
                    }
                }
            }
        });

    }

    @Override
    public ViewHolder InstantiationView(View mview) {

        IViewHolder mIViewHolder = new IViewHolder();

        mIViewHolder.img = mview.findViewById(R.id.img);
        mIViewHolder.NameText = mview.findViewById(R.id.NameText);
        mIViewHolder.TimeText = mview.findViewById(R.id.TimeText);
        mIViewHolder.okImg = mview.findViewById(R.id.okImg);
        mIViewHolder.itemView = mview.findViewById(R.id.itemView);


        mIViewHolder.Button1Text = mview.findViewById(R.id.Button1Text);
        mIViewHolder.Button2Text = mview.findViewById(R.id.Button2Text);

        return mIViewHolder;
    }

    private class IViewHolder extends ViewHolder{

        ImageView img; //图片
        TextView NameText; //名字
        TextView TimeText; //时间
        ImageView okImg; //删除按钮
        View itemView; //item

        TextView Button1Text; //重命名
        TextView Button2Text; //删除
    }

    /** 编辑 */
    public void setDeleteVisible(){

        deleteVisible = !deleteVisible;

        for (UserWorkListBean mUserWorkListBean : mData)mUserWorkListBean.setDeleteJudge(false);

        notifyDataSetChanged();
    }

    public interface deleteCallback{

        void delete(int position);

        void setName(int position,String name);
    }
}
